layui.use(['table', 'admin', 'ax','laydate','element','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
	var element = layui.element
    var form = layui.form;
	
	//tab切换
	element.on('tab(doTabBrief)',function(data){
		console.log(data.index)
	})

    laydate.render({
      elem: '#startTime'
        	,value:new Date()
    });

    laydate.render({
      elem: '#endTime'
        	,value:new Date()
    });

    laydate.render({
      elem: '#startTime1'
        	,value:new Date()
    });

    laydate.render({
      elem: '#endTime1'
        	,value:new Date()
    });

    /**
     * 运输信息表管理
     */
    var TransportMsg = {
        tableId: "transportMsgTable"
    };

    /**
     * 调度单表管理
     */
    var TDispatch = {
        tableId: "tDispatchTable"
    };

    /**
     * 调度单表管理
     */
    var TDispatch3 = {
        tableId: "tDispatchTable3"
    };

    /**
     *
     */
    var TDispatch4 = {
        tableId: "tDispatchTable4"
    };

    /**
     * 初始化表格的列
     */
    TransportMsg.initColumn = function () {
        return [[
            {field: 'id', hide: true, title: '主键id', rowspan:2},
            {field: '', title: '序号', type:'numbers', width: 120, rowspan:2},
            {field: 'privatecarFleetName', title: '承运商', sort: true, rowspan:2, width: 140},
            {field: 'v1', title: '车牌号', sort: true, rowspan:2, width: 140},
            {field: 'createUsername', title: '签到人', sort: true, rowspan:2, width: 140},
            {field: 'v2', title: '手机号', rowspan:2, width: 140},
            {field: 'times', title: '签到时间', sort: true, rowspan:2, width: 140},
            {field: 'subtime', title: '等待时长', sort: true, rowspan:2, width: 140,
            	templet: function(d){
					if(d.subtime != '' && d.subtime != null){
						return d.subtime.replace(":","时")+"分"
					} else {
						return ""
					}
            	}
			},
            {field: '', title: '门店进度', colspan:10, align: "center"}
        ],[
            {field: 'getMenDian1Tpl', title: '', templet: '#getMenDian1Tpl', width: 140, align: "center"},
            {field: 'getMenDian1Tp2', title: '', templet: '#getMenDian1Tp2', width: 140, align: "center"},
            {field: 'getMenDian1Tp3', title: '', templet: '#getMenDian1Tp3', width: 140, align: "center"},
            {field: 'getMenDian1Tp4', title: '', templet: '#getMenDian1Tp4', width: 140, align: "center"},
            {field: 'getMenDian1Tp5', title: '', templet: '#getMenDian1Tp5', width: 140, align: "center"},
            {field: 'getMenDian1Tp6', title: '', templet: '#getMenDian1Tp6', width: 140, align: "center"},
            {field: 'getMenDian1Tp7', title: '', templet: '#getMenDian1Tp7', width: 140, align: "center"},
            {field: 'getMenDian1Tp8', title: '', templet: '#getMenDian1Tp8', width: 140, align: "center"},
            {field: 'getMenDian1Tp9', title: '', templet: '#getMenDian1Tp9', width: 140, align: "center"},
            {field: 'getMenDian1Tpl0', title: '', templet: '#getMenDian1Tpl0', width: 140, align: "center"},
        ]];
    };
    
    /**
     * 初始化表格的列
     */
    TDispatch.initColumn = function () {
        return [[
            {field: '', title: '序号', type:'numbers', width: 120},
            {field: 'startDate', sort: true, title: '装车日期'},
            {field: 'carrierName', sort: true, title: '承运商'},
            {field: 'siji', title: '司机', sort: true},
            {field: 'carnumber', title: '车牌号', sort: true},
            {field: 'sijidianhua', title: '联系方式'},
            {field: 'subtime', title: '装车时长', sort: true,
            	templet: function(d){
					if(d.subtime != '' && d.subtime != null){
						return d.subtime.replace(":","时")+"分"
					} else {
						return ""
					}
            	}
			},
            {field: 'tstate', title: '装车状态', sort: true,
            	templet: function(d){
            		var str = ''
					if(d.tstate == '待定价' || d.tstate == '待审核'){
						str = '<span>装车中</span>'
					} else {
						str = '<span style="color: #43CF7C;">已完成</span>'
					}
					return str
            	}
			}
        ]];
    };
    
    /**
     * 初始化表格的列
     */
    TDispatch3.initColumn = function () {
        return [[
            {field: '', title: '序号', type:'numbers', width: 120, rowspan:2},
            {field: 'startDate', title: '发车日期', rowspan:2, sort: true},
            {field: 'carrierName', title: '承运商', rowspan:2, sort: true},
            {field: 'carnumber', title: '车牌号', rowspan:2, sort: true},
            {field: 'privatecarType', title: '车型', rowspan:2, sort: true},
            {field: 'driver1', title: '司机', rowspan:2, sort: true},
            {field: 'tel1', title: '联系方式', rowspan:2},
            {field: 'daocangTimes', title: '到仓时间', rowspan:2, sort: true},
            {field: 'sjCnt', title: '配送次数', style:'color: #43CF7C;', rowspan:2, sort: true},
            {field: 'fayunTimes', title: '发运时间', rowspan:2, sort: true},
            {field: 'fulllv', title: '满载率%', rowspan:2, sort: true},
			/*
            {align: 'center', toolbar: '#tableBar4', rowspan:2, title: '装载照片'},
			*/
            {field: 'jindu', title: '完成率%', rowspan:2, sort: true},
            {field: 'driverEvalFlag', title: '是否评价', rowspan:2},
            {field: '', title: '配送进度', colspan:20, align: "center"},
        ],[
            {field: 'getUnit', title: '', templet: '#getUnitTpl', width: 140, event: 'getUnitSign'},
            {field: 'reachTime', title: '送达时间', width: 150, style:'color: #43CF7C;', hide: true},
            {field: 'getUnit1', title: '', templet: '#getUnit1Tpl', width: 140, event: 'getUnitSign1'},
            {field: 'reachTime1', title: '送达时间', width: 150, style:'color: #43CF7C;', hide: true},
            {field: 'getUnit2', title: '', templet: '#getUnit2Tpl', width: 140, event: 'getUnitSign2'},
            {field: 'reachTime2', title: '送达时间', width: 150, style:'color: #43CF7C;', hide: true},
            {field: 'getUnit3', title: '', templet: '#getUnit3Tpl', width: 140, event: 'getUnitSign3'},
            {field: 'reachTime3', title: '送达时间', width: 150, style:'color: #43CF7C;', hide: true},
            {field: 'getUnit4', title: '', templet: '#getUnit4Tpl', width: 140, event: 'getUnitSign4'},
            {field: 'reachTime4', title: '送达时间', width: 150, style:'color: #43CF7C;', hide: true},
            {field: 'getUnit5', title: '', templet: '#getUnit5Tpl', width: 140, event: 'getUnitSign5'},
            {field: 'reachTime5', title: '送达时间', width: 150, style:'color: #43CF7C;', hide: true},
            {field: 'getUnit6', title: '', templet: '#getUnit6Tpl', width: 140, event: 'getUnitSign6'},
            {field: 'reachTime6', title: '送达时间', width: 150, style:'color: #43CF7C;', hide: true},
            {field: 'getUnit7', title: '', templet: '#getUnit7Tpl', width: 140, event: 'getUnitSign7'},
            {field: 'reachTime7', title: '送达时间', width: 150, style:'color: #43CF7C;', hide: true},
            {field: 'getUnit8', title: '', templet: '#getUnit8Tpl', width: 140, event: 'getUnitSign8'},
            {field: 'reachTime8', title: '送达时间', width: 150, style:'color: #43CF7C;', hide: true},
            {field: 'getUnit9', title: '', templet: '#getUnit9Tpl', width: 140, event: 'getUnitSign9'},
            {field: 'reachTime9', title: '送达时间', width: 150, style:'color: #43CF7C;', hide: true},
        ]];
    };

    TDispatch4.initColumn = function () {
        return [[
            {field: '', title: '序号', type:'numbers', width: 120, rowspan:2},
            {field: 'carnumber', title: '车牌号', rowspan:2, sort: true},
            {field: 'siji', title: '司机联系方式', rowspan:2, sort: true},
            {field: 'estDeliveryDate', title: '发运日期', rowspan:2, sort: true},
            {field: 'remainKilometer', title: '剩余公里数', rowspan:2, sort: true},
            {field: 'remainTime', title: '剩余时间', rowspan:2, sort: true},
            {field: 'createTime', title: '轨迹时间', rowspan:2, sort: true},
            {field: 'operateName', title: '项目', rowspan:2, sort: true},

        ]];
    };

    /**
     * 点击查询按钮
     */
    TransportMsg.search = function () {
        var queryData = {};
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['operateId'] = $("#operateId").val();
        queryData['carrierId'] = $("#carrierId").val();
        queryData['condition'] = $("#condition").val();
        table.reload(TransportMsg.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TransportMsg.tableId,
        url: Feng.ctxPath + '/tDispatch/list_driverSignin2',
        page: false,
        limit: 1000,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: TransportMsg.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TransportMsg.search();
        TDispatch.search();
        TDispatch3.search();
        TDispatch4.search();
    });
    $('#btnSearch1').click(function () {
        TDispatch4.search();
    });

    /**
     * 点击查询按钮
     */
    TDispatch.search = function () {
        var queryData = {};
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['operateId'] = $("#operateId").val();
        queryData['carrierId'] = $("#carrierId").val();
        queryData['condition'] = $("#condition").val();
        table.reload(TDispatch.tableId, {
            where: queryData, page: {curr: 1},
			done: function (res, curr, count) {
			}
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TDispatch.tableId,
        url: Feng.ctxPath + '/tDispatch/hx_zhuangchejindu',
        page: false,
        limit: 1000,
        height: "full-60",
        toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
        defaultToolbar: ['filter', 'exports', 'print'],
        cellMinWidth: 100,
        cols: TDispatch.initColumn(),
    });

    /**
     * 点击查询按钮
     */
    TDispatch3.search = function () {
        var queryData = {};
        queryData['operateId'] = $("#operateId").val();
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['carrierId'] = $("#carrierId").val();
        queryData['getUnit'] = $("#getUnit").val();
        queryData['condition'] = $("#condition").val();
        table.reload(TDispatch3.tableId, {
            where: queryData, page: {curr: 1},
			done: function (res, curr, count) {
			}
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TDispatch3.tableId,
        url: Feng.ctxPath + '/tDispatch/hx_peisongjindu',
        page: false,
        limit: 1000,
        height: "full-60",
        toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
        defaultToolbar: ['filter', 'exports', 'print'],
        cellMinWidth: 100,
        cols: TDispatch3.initColumn(),
    });
    /**
     * 点击查询按钮
     */
    TDispatch4.search = function () {
        var queryData = {};
        queryData['operateId'] = $("#operateId1").val();
        queryData['startTime'] = $("#startTime1").val();
        queryData['endTime'] = $("#endTime1").val();
        queryData['siji'] = $("#siji").val();
        queryData['carnumber'] = $("#carnumber").val();
        queryData['condition'] = $("#condition1").val();
        table.reload(TDispatch4.tableId, {
            where: queryData, page: {curr: 1},
			done: function (res, curr, count) {
			}
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TDispatch4.tableId,
        url: Feng.ctxPath + '/tDispatch/hx_daocanggenzong',
        page: false,
        limit: 1000,
        height: "full-60",
        toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
        defaultToolbar: ['filter', 'exports', 'print'],
        cellMinWidth: 100,
        cols: TDispatch4.initColumn()
    });

    // 工具条点击事件
    table.on('tool(' + TDispatch3.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'paymentProve') {
  	    	//构建图像数组
      		var items = [];
        	if (data.zhuangzaiFileid!='') {
        		var obj = {};
        		obj.src = '/system/previewa/' + data.zhuangzaiFileid;
        		obj.title = '预览图片';
        		items.push(obj)
    	      	//定义选项（如果需要）
    	      	var options = {
    	      	    index: 0 // 此选项表示您将从第一张图片开始
    	      	};
    	      	//初始化插件
    	      	var viewer = new PhotoViewer(items, options);
			} else {
                Feng.info("暂无照片!");
			}
        } else if (layEvent === 'getUnitSign') {
			if(data.reachTime != null && data.reachTime != ''){
				var that = this
				var nodes = '<span>'+data.reachTime+'</span><br>';
				if(data.subtime != null && data.subtime != ''){
					if(data.handoverTime >= 2){
						nodes += '<span style="color: #FF4500;">交接用时：'+data.subtime.replace(":","时")+'分</span>';
					} else {
						nodes += '<span style="color: #43CF7C;">交接用时：'+data.subtime.replace(":","时")+'分</span>';
					}
				} else {
					nodes += '<span>交接用时：——</span>';
				}
				layer.tips(nodes, that);
			}
        } else if (layEvent === 'getUnitSign1') {
			if(data.reachTime1 != null && data.reachTime1 != ''){
				var that = this
				var nodes = '<span>'+data.reachTime1+'</span><br>';
				if(data.subtime1 != null && data.subtime1 != ''){
					if(data.handoverTime1 >= 2){
						nodes += '<span style="color: #FF4500;">交接用时：'+data.subtime1.replace(":","时")+'分</span>';
					} else {
						nodes += '<span style="color: #43CF7C;">交接用时：'+data.subtime1.replace(":","时")+'分</span>';
					}
				} else {
					nodes += '<span>交接用时：——</span>';
				}
				layer.tips(nodes, that);
			}
        }  else if (layEvent === 'getUnitSign2') {
			if(data.reachTime2 != null && data.reachTime2 != ''){
				var that = this
				var nodes = '<span>'+data.reachTime2+'</span><br>';
				if(data.subtime2 != null && data.subtime2 != ''){
					if(data.handoverTime2 >= 2){
						nodes += '<span style="color: #FF4500;">交接用时：'+data.subtime2.replace(":","时")+'分</span>';
					} else {
						nodes += '<span style="color: #43CF7C;">交接用时：'+data.subtime2.replace(":","时")+'分</span>';
					}
				} else {
					nodes += '<span>交接用时：——</span>';
				}
				layer.tips(nodes, that);
			}
        }  else if (layEvent === 'getUnitSign3') {
			if(data.reachTime3 != null && data.reachTime3 != ''){
				var that = this
				var nodes = '<span>'+data.reachTime3+'</span><br>';
				if(data.subtime3 != null && data.subtime3 != ''){
					if(data.handoverTime3 >= 2){
						nodes += '<span style="color: #FF4500;">交接用时：'+data.subtime3.replace(":","时")+'分</span>';
					} else {
						nodes += '<span style="color: #43CF7C;">交接用时：'+data.subtime3.replace(":","时")+'分</span>';
					}
				} else {
					nodes += '<span>交接用时：——</span>';
				}
				layer.tips(nodes, that);
			}
        }  else if (layEvent === 'getUnitSign4') {
			if(data.reachTime4 != null && data.reachTime4 != ''){
				var that = this
				var nodes = '<span>'+data.reachTime4+'</span><br>';
				if(data.subtime4 != null && data.subtime4 != ''){
					if(data.handoverTime4 >= 2){
						nodes += '<span style="color: #FF4500;">交接用时：'+data.subtime4.replace(":","时")+'分</span>';
					} else {
						nodes += '<span style="color: #43CF7C;">交接用时：'+data.subtime4.replace(":","时")+'分</span>';
					}
				} else {
					nodes += '<span>交接用时：——</span>';
				}
				layer.tips(nodes, that);
			}
        }  else if (layEvent === 'getUnitSign5') {
			if(data.reachTime5 != null && data.reachTime5 != ''){
				var that = this
				var nodes = '<span>'+data.reachTime5+'</span><br>';
				if(data.subtime5 != null && data.subtime5 != ''){
					if(data.handoverTime5 >= 2){
						nodes += '<span style="color: #FF4500;">交接用时：'+data.subtime5.replace(":","时")+'分</span>';
					} else {
						nodes += '<span style="color: #43CF7C;">交接用时：'+data.subtime5.replace(":","时")+'分</span>';
					}
				} else {
					nodes += '<span>交接用时：——</span>';
				}
				layer.tips(nodes, that);
			}
        }  else if (layEvent === 'getUnitSign6') {
			if(data.reachTime6 != null && data.reachTime6 != ''){
				var that = this
				var nodes = '<span>'+data.reachTime6+'</span><br>';
				if(data.subtime6 != null && data.subtime6 != ''){
					if(data.handoverTime6 >= 2){
						nodes += '<span style="color: #FF4500;">交接用时：'+data.subtime6.replace(":","时")+'分</span>';
					} else {
						nodes += '<span style="color: #43CF7C;">交接用时：'+data.subtime6.replace(":","时")+'分</span>';
					}
				} else {
					nodes += '<span>交接用时：——</span>';
				}
				layer.tips(nodes, that);
			}
        }  else if (layEvent === 'getUnitSign7') {
			if(data.reachTime7 != null && data.reachTime7 != ''){
				var that = this
				var nodes = '<span>'+data.reachTime7+'</span><br>';
				if(data.subtime7 != null && data.subtime7 != ''){
					if(data.handoverTime7 >= 2){
						nodes += '<span style="color: #FF4500;">交接用时：'+data.subtime7.replace(":","时")+'分</span>';
					} else {
						nodes += '<span style="color: #43CF7C;">交接用时：'+data.subtime7.replace(":","时")+'分</span>';
					}
				} else {
					nodes += '<span>交接用时：——</span>';
				}
				layer.tips(nodes, that);
			}
        }  else if (layEvent === 'getUnitSign8') {
			if(data.reachTime8 != null && data.reachTime8 != ''){
				var that = this
				var nodes = '<span>'+data.reachTime8+'</span><br>';
				if(data.subtime8 != null && data.subtime8 != ''){
					if(data.handoverTime8 >= 2){
						nodes += '<span style="color: #FF4500;">交接用时：'+data.subtime8.replace(":","时")+'分</span>';
					} else {
						nodes += '<span style="color: #43CF7C;">交接用时：'+data.subtime8.replace(":","时")+'分</span>';
					}
				} else {
					nodes += '<span>交接用时：——</span>';
				}
				layer.tips(nodes, that);
			}
        }  else if (layEvent === 'getUnitSign9') {
			if(data.reachTime9 != null && data.reachTime9 != ''){
				var that = this
				var nodes = '<span>'+data.reachTime9+'</span><br>';
				if(data.subtime9 != null && data.subtime9 != ''){
					if(data.handoverTime9 >= 2){
						nodes += '<span style="color: #FF4500;">交接用时：'+data.subtime9.replace(":","时")+'分</span>';
					} else {
						nodes += '<span style="color: #43CF7C;">交接用时：'+data.subtime9.replace(":","时")+'分</span>';
					}
				} else {
					nodes += '<span>交接用时：——</span>';
				}
				layer.tips(nodes, that);
			}
        } 
    });

    //查询库区
    form.on('select(operateId)',function(){
        $("#carrierId").empty();
        $.ajax({
            url:Feng.ctxPath + '/basicCarrier/Querylist',
            dataType:"json",   //返回格式为json
            async: false,
            data:{"operateId":$("#operateId").val()},    //参数值
            type:"post",   //请求方式
            success:function(data){
                if(data.length>0){
                    var str = "<option value=''>选择承运商</option>";
                    $.each(data,function(key,value){
                        str += "<option value='"+value.id+"'>"+value.carrierName+"</option>";
                    });
                    $("#carrierId").html(str);
                }
            }
        });
        form.render('select');
    });

});
